package se.kth.id2212.market;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

import se.kth.id2212.bankjdbc.Bank;
import se.kth.id2212.bankjdbc.BankImpl;

public class Server {
    private static final String USAGE="java bankrmi.Server <bank_rmi_url>";
    private static final String MARKET="eBAY";
    private static final String BANK="Nordea";
    private static final String DATASOURCE = "Banks";
    private static final String DBMS = "derby";
    
    public Server(String marketName, String bankName, String datasource, String dbms) {
        try {
            Market marketobj = new MarketImpl(marketName,datasource, dbms);
            try{
            	LocateRegistry.getRegistry().list();
            }catch(RemoteException e){
            	LocateRegistry.createRegistry(1099);
            }
            
            java.rmi.Naming.rebind(marketName, marketobj);
            
            System.out.println(marketName + "is ready");
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        if(args.length > 4 || (args.length> 0 && args[0].equalsIgnoreCase("-h"))){
            System.out.println(USAGE);
            System.exit(1);
        }
        String marketName = null;
        String bankName = null;
        String datasource = null;
        String dbms = null;
        if(args.length ==4){
            marketName = args[3];
            bankName = args[0];
            datasource = args[1];
            dbms = args[2];
        }else{
            marketName = MARKET;
            bankName= BANK;
            datasource = DATASOURCE;
            dbms = DBMS;
        }       
        new Server(marketName, bankName,datasource,dbms);
        
        System.out.println("Market server start");
        
    }
}
